#include <string.h>
#include <string>
#include "com_le_http_lehttprequest_LeHttpRequest.h"
#include "LTCurlRequestEx.h"

#include <android/log.h>

#define UTF_8 "UTF-8"
#define GBK "GBK"

#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "LeHttpRequest", __VA_ARGS__)

static char* jstringToChar(JNIEnv* env, jstring jstr, const char *pEncode)
{
  char* rtn = NULL;
  if ( jstr == NULL )
  {
    rtn = new char[1];
    rtn[0] = '\0';
    return rtn;
  }
  jclass clsstring = env->FindClass("java/lang/String");
  //jstring strencode = env->NewStringUTF("utf-8");
  jstring strencode = env->NewStringUTF(pEncode);
  jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
  jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
  jsize alen = env->GetArrayLength(barr);
  if(alen <= 0)
  {
    env->DeleteLocalRef(barr);
    env->DeleteLocalRef(strencode);
    env->DeleteLocalRef(clsstring);

    rtn = new char[1];
    rtn[0] = '\0';
    return rtn;
  }

  jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);

  rtn = new char[alen + 1];
  memcpy(rtn, ba, alen);
  rtn[alen] = 0;

  env->ReleaseByteArrayElements(barr, ba, 0);
  env->DeleteLocalRef(barr);
  env->DeleteLocalRef(strencode);
  env->DeleteLocalRef(clsstring);

  return rtn;
}

static jstring charToJstring(JNIEnv* env, const char* pat, const char *pEncode)
{
  //jclass strClass = env->FindClass("Ljava/lang/String;");
  jclass strClass = env->FindClass("java/lang/String");
  jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
  jbyteArray bytes = env->NewByteArray(strlen(pat));
  env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
  //jstring encoding = env->NewStringUTF("utf-8");
  jstring encoding = env->NewStringUTF(pEncode);
  jstring strRet = (jstring)env->NewObject(strClass, ctorID, bytes, encoding);;
  env->DeleteLocalRef(bytes);
  env->DeleteLocalRef(encoding);
  env->DeleteLocalRef(strClass);
  return strRet;
}

/*
 * Class:     com_le_http_lehttprequest_LeHttpRequest
 * Method:    get
 * Signature: (Ljava/lang/String;Ljava/util/HashMap;Ljava/lang/String;IJLjava/util/logging/Handler;)I
 */
JNIEXPORT jint JNICALL Java_com_le_http_lehttprequest_LeHttpRequest_get(JNIEnv *env, jclass classType, jstring url, jobject header, jstring proxy, jint tag, jlong timeout, jobject handler)
{
    LTCurlRequestEx *request = LTCurlRequestEx::autoReleasedRequest();

    char* urlStr = jstringToChar(env, url, UTF_8);
    char* proxyStr = jstringToChar(env, proxy, UTF_8);

    LOGV("url=%s\n",urlStr);
    request->get(urlStr,timeout,NULL,proxyStr,NULL,
    [=](void *data, size_t length, bool success, long requestId) {
      if (success && data != NULL && length > 0) {
        LOGV("%s\n",(char*)data);
      } else {

      }
    });

    delete[] urlStr;
    delete[] proxyStr;
  return 0;
}

/*
 * Class:     com_le_http_lehttprequest_LeHttpRequest
 * Method:    post
 * Signature: (Ljava/lang/String;Ljava/util/HashMap;[BLjava/lang/String;IJLjava/util/logging/Handler;)I
 */
JNIEXPORT jint JNICALL Java_com_le_http_lehttprequest_LeHttpRequest_post(JNIEnv *env, jclass classType, jstring url, jobject header, jbyteArray data, jstring proxy, jint tag, jlong timeout, jobject handler)
{
  return 0;
}

/*
 * Class:     com_le_http_lehttprequest_LeHttpRequest
 * Method:    calcleWithRequestId
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_com_le_http_lehttprequest_LeHttpRequest_cancleWithRequestId(JNIEnv *env, jclass classType, jint id)
{

}

/*
 * Class:     com_le_http_lehttprequest_LeHttpRequest
 * Method:    calcleWithTag
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_com_le_http_lehttprequest_LeHttpRequest_cancleWithTag(JNIEnv *env, jclass classType, jint tag)
{

}


